package com.ctgu.ImageRecognition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ctgu.ImageRecognition.dao.PhototagDao;
import com.ctgu.ImageRecognition.dao.TagDao;
import com.ctgu.ImageRecognition.entity.Photo;
import com.ctgu.ImageRecognition.dao.PhotoDao;
import com.ctgu.ImageRecognition.entity.Phototag;
import com.ctgu.ImageRecognition.entity.Tag;
import com.ctgu.ImageRecognition.service.PhotoService;
import com.ctgu.ImageRecognition.util.QiniuUploadUtils;
import com.ctgu.ImageRecognition.vo.Labels;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Photo)表服务实现类
 *
 * @author 
 * @since 2023-07-05 18:31:03
 */
//@Service("photoService")
@Service
public class PhotoServiceImpl implements PhotoService {
    @Resource
    private PhotoDao photoDao;
    @Resource
    private TagDao tagDao;
    @Resource
    private PhototagDao phototagDao;


    @Override
    public List<Photo> listPhoto() {
        return photoDao.selectList(null);
    }

    @Transactional
    @Override
    public Boolean uploadPhoto(Photo photo, MultipartFile file) {
        String photoPath = (String) QiniuUploadUtils.uploadImage(file).getData();
        photo.setFilePath(photoPath);
        int result = photoDao.insert(photo);
        if(result==0) return false;

        return true;
    }

    @Override
    public int updatePhoto(Photo photo) {

//        System.out.println("photo.toString() = " + photo.toString());
        UpdateWrapper<Photo> update = new UpdateWrapper();
        update.eq("p_id",photo.getPId()).set("p_name",photo.getPName());
        return photoDao.update(null,update);
    }

    @Transactional
    @Override
    public int deletePhoto(Integer id) {
        QueryWrapper deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("p_id",id);
        Photo selectPhoto = photoDao.selectOne(deleteWrapper);
        int resultNum = photoDao.delete(deleteWrapper);
        return resultNum;
    }

    @Override
    public int collectPhoto(Integer id) {
        QueryWrapper queryWrap = new QueryWrapper();
        queryWrap.eq("p_id",id);
        Photo selectPhoto = photoDao.selectOne(queryWrap);


        UpdateWrapper update = new UpdateWrapper();
        update.set("is_collection",!selectPhoto.getIsCollection());
        update.eq("p_id",id);


        return photoDao.update(null,update);
    }

    @Transactional
    @Override
    public void insertPhoto(String imageUrl, Labels label, int uId) {

        Photo photo = new Photo();
        photo.setFilePath(imageUrl);
        photo.setPName(label.getName());
        photo.setPhotoUploaded(new Date());
        photo.setIsCollection(false);
        photo.setUId(uId);
        photoDao.insert(photo);
        int pId =photo.getPId();
        int tId;
//        QueryWrapper<Tag> selectWrapper = new QueryWrapper<>();
//        selectWrapper.eq("t_name",label.getFirstCategory());
//        selectWrapper.eq("user_id",uId);
//        Tag selectTag =tagDao.selectOne(selectWrapper);
        Tag selectTag = tagDao.selectByName(uId,label.getFirstCategory());
        System.out.println("找到t_name:"+selectTag==null);
        if (selectTag!= null) {
            System.out.println("输出ID"+ selectTag.getTId());
            tId= selectTag.getTId();
        }else {
            Tag tag = new Tag();
            tag.setTName(label.getFirstCategory());
            tag.setTCreated(new Date());
            tag.setUserId(uId);
            tagDao.insert(tag);
            tId=tag.getTId();
        }
        Phototag phototag = new Phototag();
        phototag.setTId(tId);
        phototag.setPId(pId);
        phototagDao.insert(phototag);
        return;
    }

    @Override
    public List<Photo> queryCollection() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_collection",1);
        return photoDao.selectList(queryWrapper);
    }

    @Transactional
    @Override
    public Boolean deletePhoto(String filePath) {
        QueryWrapper photoWrapper = new QueryWrapper();
        photoWrapper.eq("file_path",filePath);
//        System.out.println("photoDao.selectOne(photoWrapper) = " + photoDao.selectOne(photoWrapper));
        Photo selectPhoto=photoDao.selectOne(photoWrapper);
        QueryWrapper deleteWrapper = new QueryWrapper();
        deleteWrapper.eq("p_id",selectPhoto.getPId());
        //删除photo表
        photoDao.delete(deleteWrapper);
        //删除phototaf表
        phototagDao.delete(deleteWrapper);

        return true;
    }

}
